package dz.chuxin.app.sample;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import dz.chuxin.app.core.BaseSectionRecyclerActivity;
import dz.chuxin.framework.widget.recycler.BaseViewHolder;
import dz.chuxin.framework.widget.recycler.ChuXinRecyclerView;
import dz.chuxin.framework.widget.recycler.SectionModel;

/**
 * Created by chuxin on 2016/7/1.
 */
public class SectionRecyclerSampleActivity extends BaseSectionRecyclerActivity<String> {

    @Override
    protected void setUpToolbarComplete(Toolbar toolbar) {
        toolbar.setTitle("分组列表");
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        super.setUpData(savedInstanceState);
        mRecyclerView.setRefreshingOn();
    }

    @Override
    protected BaseViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
        return new SampleViewHolder(getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false));
    }

    @Override
    public void onRefresh(final int action) {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (action == ChuXinRecyclerView.ACTION_PULL_TO_REFRESH) {
                    mDataList.clear();
                }
                int size = mDataList.size();
                mDataList.add(new SectionModel<String>(true, size, "header " + size));
                for (int i = size; i < size + 20; i++) {
                    mDataList.add(new SectionModel<>("sample list item" + i));
                }
                mRecyclerAdapter.notifyDataSetChanged();
                mRecyclerView.onRefreshComplete();
                if (mDataList.size() < 100) {
                    mRecyclerView.enableLoadMore(true);
                } else {
                    mRecyclerView.enableLoadMore(false);
                }
            }
        }, 3000);
    }

    class SampleViewHolder extends BaseViewHolder {

        TextView mTextView1;
        TextView mTextView2;

        public SampleViewHolder(View itemView) {
            super(itemView);
            mTextView1 = (TextView) itemView.findViewById(android.R.id.text1);
            mTextView2 = (TextView) itemView.findViewById(android.R.id.text2);
        }

        @Override
        public void onBindViewHolder(int position) {
            mTextView1.setText(mDataList.get(position).data);
            mTextView2.setText(mDataList.get(position).data + ">>>");
        }
    }
}
